package com.dwd.hrm.controller;

import com.alibaba.fastjson.JSON;
import com.dwd.hrm.domain.VipUser;
import com.dwd.hrm.query.LoginUserQuery;
import com.dwd.hrm.service.ILoginUserService;
import com.dwd.hrm.domain.LoginUser;

import com.baomidou.mybatisplus.plugins.Page;
import com.dwd.hrm.util.AjaxResult;
import com.dwd.hrm.util.PageList;
import com.dwd.hrm.vo.AccessTokenVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/loginUser")
public class LoginUserController {
    @Autowired
    public ILoginUserService loginUserService;

    @PostMapping("/save")
    public AjaxResult save(@RequestBody VipUser vipUser) {
        LoginUser loginUser = loginUserService.insert(vipUser);
        String json = JSON.toJSONString(loginUser);
        return AjaxResult.me().setResultObj(json);
    }


    /**
     * 无感刷新
     *
     * @param refreshToken
     * @param type
     * @return
     */
    @RequestMapping(value = "/refresh")
    public AjaxResult refresh(@RequestParam("refreshToken") String refreshToken, @RequestParam("type") Integer type) {
        AccessTokenVo accessTokenVo = loginUserService.refresh(refreshToken, type);
        return AjaxResult.me().setResultObj(accessTokenVo);
    }


    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginUser loginUser) {
        return loginUserService.login(loginUser);
    }

    /**
     * 保存和修改公用的
     *
     * @param loginUser 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody LoginUser loginUser) {
        try {
            if (loginUser.getId() != null) {
                loginUserService.updateById(loginUser);
            } else {
                loginUserService.insert(loginUser);
            }
            return AjaxResult.me().setResultObj(loginUser.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！" + e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            loginUserService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！" + e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public LoginUser get(@PathVariable("id") Long id) {
        return loginUserService.selectById(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping()
    public List<LoginUser> list() {

        return loginUserService.selectList(null);
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public PageList<LoginUser> json(@RequestBody LoginUserQuery query) {
        Page<LoginUser> page = new Page<LoginUser>(query.getPage(), query.getRows());
        page = loginUserService.selectPage(page);
        return new PageList<LoginUser>(page.getTotal(), page.getRecords());
    }
}
